﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ParseTimetable
{
    class ParseDataFromPage
    {
        public static ClassTime GetClassTime(HtmlDocument dom)
        {
            string xy = GetDropdownSelected(dom.GetElementById("xy"));
            string nj = GetDropdownSelected(dom.GetElementById("nj"));
            string xz = GetDropdownSelected(dom.GetElementById("ddlXZ"));
            string zy = GetDropdownSelected(dom.GetElementById("zy"));
            string kb = GetDropdownSelected(dom.GetElementById("kb"));

            ClassTime ct = new ClassTime(kb, nj, xy, xz, zy);

            HtmlElement table6 = dom.GetElementById("table6");
            GetClasstime(table6, ct);
            return ct;

        }

        private static string GetDropdownSelected(HtmlElement hd)
        {
            for (int i = 0; i < hd.Children.Count; i++)
            {
                if (hd.Children[i].GetAttribute("selected") == "True")
                {
                    return hd.Children[i].InnerText.Trim();
                }
            }
            return "";
        }

        private static void GetClasstime(HtmlElement ht, ClassTime ct)
        {
            HtmlElement tbody = ht.Children[0];
            HtmlElement oneLesson = tbody.Children[2];
            for (int i = 2; i <= 6; i++)
            {
                if (oneLesson.Children[i].GetAttribute("rowspan") == "2")
                {
                    switch (i)
                    {
                        case 2:
                            ct.MondayOne = oneLesson.Children[i].InnerText;
                            break;
                        case 3:
                            ct.TuesdayOne = oneLesson.Children[i].InnerText;
                            break;
                        case 4:
                            ct.WednesdayOne = oneLesson.Children[i].InnerText;
                            break;
                        case 5:
                            ct.ThursdayOne = oneLesson.Children[i].InnerText;
                            break;
                        case 6:
                            ct.FridayOne = oneLesson.Children[i].InnerText;
                            break;
                        default:
                            break;
                    }
                }
            }

            HtmlElement twoLesson = tbody.Children[4];
            for (int i = 1; i <= 5; i++)
            {
                if (twoLesson.Children[i].GetAttribute("rowspan") == "2")
                {
                    switch (i)
                    {
                        case 1:
                            ct.MondayTwo = twoLesson.Children[i].InnerText;
                            break;
                        case 2:
                            ct.TuesdayTwo = twoLesson.Children[i].InnerText;
                            break;
                        case 3:
                            ct.WednesdayTwo = twoLesson.Children[i].InnerText;
                            break;
                        case 4:
                            ct.ThursdayTwo = twoLesson.Children[i].InnerText;
                            break;
                        case 5:
                            ct.FridayTwo = twoLesson.Children[i].InnerText;
                            break;
                        default:
                            break;
                    }
                }
            }

            HtmlElement threeLesson = tbody.Children[7];
            for (int i = 2; i <= 6; i++)
            {
                if (threeLesson.Children[i].GetAttribute("rowspan") == "2")
                {
                    switch (i)
                    {
                        case 2:
                            ct.MondayThree = threeLesson.Children[i].InnerText;
                            break;
                        case 3:
                            ct.TuesdayThree = threeLesson.Children[i].InnerText;
                            break;
                        case 4:
                            ct.WednesdayThree = threeLesson.Children[i].InnerText;
                            break;
                        case 5:
                            ct.ThursdayThree = threeLesson.Children[i].InnerText;
                            break;
                        case 6:
                            ct.FridayThree = threeLesson.Children[i].InnerText;
                            break;
                        default:
                            break;
                    }
                }
            }

            HtmlElement fourLesson = tbody.Children[9];
            for (int i = 1; i <= 5; i++)
            {
                if (fourLesson.Children[i].GetAttribute("rowspan") == "2")
                {
                    switch (i)
                    {
                        case 1:
                            ct.MondayFour = fourLesson.Children[i].InnerText;
                            break;
                        case 2:
                            ct.TuesdayFour = fourLesson.Children[i].InnerText;
                            break;
                        case 3:
                            ct.WednesdayFour = fourLesson.Children[i].InnerText;
                            break;
                        case 4:
                            ct.ThursdayFour = fourLesson.Children[i].InnerText;
                            break;
                        case 5:
                            ct.FridayFour = fourLesson.Children[i].InnerText;
                            break;
                        default:
                            break;
                    }
                }
            }

            HtmlElement fiveLesson = tbody.Children[12];
            for (int i = 2; i <= 6; i++)
            {
                if (fiveLesson.Children[i].GetAttribute("rowspan") == "2")
                {
                    switch (i)
                    {
                        case 2:
                            ct.MondayFive = fiveLesson.Children[i].InnerText;
                            break;
                        case 3:
                            ct.TuesdayFive = fiveLesson.Children[i].InnerText;
                            break;
                        case 4:
                            ct.WednesdayFive = fiveLesson.Children[i].InnerText;
                            break;
                        case 5:
                            ct.ThursdayFive = fiveLesson.Children[i].InnerText;
                            break;
                        case 6:
                            ct.FridayFive = fiveLesson.Children[i].InnerText;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}
